package com.mastercard.assets.web.struts.actions;

import com.mastercard.assets.MasterCardException;
import com.mastercard.assets.jpa.TblMaterialAsset;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.FormasForm;
import com.mastercard.assets.web.util.to.UserLogin;
import java.io.IOException;
import java.util.List;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class MaterialesIndex extends Action {

    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException, ServletException {

        HttpSession session = request.getSession();

        // en caos de null
        if (session == null) {
            return mapping.findForward("noLogin");
        }

        // toma el userLogin
        UserLogin user = (UserLogin) session.getAttribute("UserLogin");

        // en caso de null
        if (user == null) {
            return mapping.findForward("noLogin");
        }

        FormasForm f = (FormasForm) form;

        int cveForma = f.getCveForma();
        int operation = f.getOperacion();
        int cveAsset = f.getCveAsset();
        int result = 0;
        String nombre = "";
        File materialdeassetDir = new File( MCUtil.getMaterialDeAssetDir( ) ) ;
        
        if(operation==2){
                    try {
                        List< TblMaterialAsset > Materiales = MasterCardDAO.getAllActiveMateriales( cveAsset ) ;
                        
                        for ( TblMaterialAsset as : Materiales ){
                            if(as.getCveMaterial()==cveForma){
                                nombre = as.getArchivoMaterial();
                                break;
                            }
                        }
                        
                        String filePath = MCUtil.extractMaterialAssetFileName( cveAsset , nombre , "["+cveForma+"]" ) ;
                        File fichero = new File(materialdeassetDir ,filePath);
                        boolean mtf = fichero.delete();
                        boolean mt  = MasterCardDAO.removeFileFromMaterial( cveForma ) ;
                        result = 1;
                    } catch (MasterCardException ex) {
                        MasterCardDAO.closeEntityManager( ) ;
                        Logger.getLogger(AssetsMaterialesRegistrar.class.getName()).log(Level.SEVERE, null, ex);
                    }
        }
      
        request.setAttribute("result" ,  result  ) ; 
        MasterCardDAO.closeEntityManager( ) ;
        return mapping.findForward("success");

    }
}
